import React from "react";
import { sleep } from 'antd-mobile/es/utils/sleep'
import styles from '@/assets/css/order.module.css';

const OrderIndex = () => {
    const [list, SetList] = React.useState<any>([])
    const [hasMore, SethasMore] = React.useState(false)
    const [page, SetPage] = React.useState(1)
    const [status, SetStatus] = React.useState('all')

    React.useEffect(() => 
    {
        GetOrderData()
    }, [])

    React.useEffect(() => 
    {
        OnRefresh();
    }, [status])

    //下拉刷新
    const OnRefresh = async () => 
    {

        SetPage(1)
        SethasMore(true);
        SetList([]);

        await sleep(1000);
        await GetOrderData();
    };

    //上拉加载
    const LoadMore = async () => 
    {
        await sleep(1000)
        GetOrderData()
    }

    //请求数据的方法
    const GetOrderData = async () => 
    {
        let result = await React.HTTP.post('order/OrderIndex', {
            busid: React.Business.id,
            page: page,
            status: status
        })

        if (result.code == 0) {
            //没有更多数据
            SethasMore(false);
            return false
        }
        else 
        {
            //设置分页和列表合并
            SethasMore(true)
            SetList(list.concat(result.data))
            SetPage(page + 1)
        }
    }

    // 申请退款
    const ApplyRefund = async (orderid: number) =>
    {
        let res = await React.UI.Dialog.confirm({
            title: '退出',
            content: '是否确认申请退款'
        })

        if(!res) return false

       let result = await React.HTTP.post('/order/ApplyRefund', {
            busid: React.Business.id,
            orderid: orderid
       })

        if(result.code == 0)
        {
            React.error(result.msg, () => {})
            return false
        }
        else
        {
            React.success(result.msg, () => {
                OnRefresh()
            })
        }
    }

    // 按钮结构
    const ButtonList = (order: any) => 
    {
        if (order.status == '3' && !order.rate) //未支付
        {
            return (
                <React.UI.Button color='warning' size='small' onClick={() => React.navigate(`/order/rate?orderid=${order.id}`)}>立即评价
                </React.UI.Button>
            )
        }
        else if (order.status == 1) //已支付
        {
            return (
                <React.UI.Button color='danger' size='small' onClick={() => ApplyRefund(order.id)}>申请退款</React.UI.Button>
            )
        }
    }
    return (
        <>
            {/* 选项卡切换 */}
            <React.UI.Tabs onChange={(val: string) => SetStatus(val)} activeKey={status} activeLineMode={'full'}>
                <React.UI.Tabs.Tab title="全部" key="all" />
                <React.UI.Tabs.Tab title="已支付" key="1" />
                <React.UI.Tabs.Tab title="已入住" key="2" />
                <React.UI.Tabs.Tab title="已退房" key="3" />
                <React.UI.Tabs.Tab title="已评价" key="4" />
                <React.UI.Tabs.Tab title="售后" key="-1" />
            </React.UI.Tabs>

            {/* 下拉刷新列表 */}
            <React.UI.PullToRefresh onRefresh={OnRefresh}>
                {/* 订单列表 */}
                <div className={styles.order_list}>
                    {list && list.map((item: any, key: number) =>
                        <div className={styles.item} key={key}>
                            <div className={styles.item_top}>
                                <p>{item.room.name}</p>
                                <div className={styles.top_tag}>{item.status_text}</div>
                            </div>

                            <div className={styles.house}>
                                <div className={styles.item_swipers}>
                                    <img src={item.room.thumb_text} alt="" />
                                </div>
                                <div className={styles.item_times}>
                                    <div>
                                        <div>{item.starttime_text}</div>
                                        <div>{item.startday_text}</div>
                                    </div>
                                    <div>
                                        <div>共{item.order_day}晚</div>
                                        <div className={styles.item_right}>
                                            <img src={require("@/assets/images/right1.png")} alt="" />
                                        </div>
                                    </div>
                                    <div>
                                        <div>{item.endtime_text}</div>
                                        <div>{item.endday_text}</div>
                                    </div>
                                    <div>
                                        <div className={styles.item_pay}>订单总价</div>
                                        <div className={styles.item_price}>￥{item.price}</div>
                                    </div>
                                </div>
                            </div>

                            <div className={styles.item_bar}>

                                {ButtonList(item)}

                                <React.UI.Button color='primary' size='small' onClick={() => React.navigate(`/order/info?orderid=${item.id}`)}>查看详情
                                </React.UI.Button>
                            </div>
                        </div>
                    )}
                </div>

                {/* 上拉加载 */}
                <React.UI.InfiniteScroll loadMore={LoadMore} hasMore={hasMore} threshold={0} />
            </React.UI.PullToRefresh>
        </>
    )
}

export default OrderIndex;